﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class CustomerProductApp
    {
        private CustomerProductRepository _customerProductRepository;
        private ProductRepository _productRepository;

        public CustomerProductApp()
        {
            _customerProductRepository = new CustomerProductRepository();
            _productRepository = new ProductRepository();
        }

        /// <summary>
        /// 查询客户对应产品数据【全部产品】
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public List<ProductView> GetList(Guid cusID)
        {
            IEnumerable<CustomerProduct> cusProList = _customerProductRepository.IQueryable(a => a.CustomerID == cusID);
            //IEnumerable<Product> proList = _productRepository.IQueryable(a => a.Type == type && a.Status == 1);
            IEnumerable<Product> proList = _productRepository.IQueryable(a => a.Status == 1);
            List<ProductView> pViewList = new List<ProductView>();
            foreach (Product pro in proList)
            {
                ProductView pw = new ProductView();
                pw.Id = pro.Id;
                pw.Name = pro.Name;
                pw.Number = pro.Number;
                //pw.Type = pro.Type;
                pw.Price = pro.Price;

                foreach (CustomerProduct cp in cusProList)
                {
                    if (cp.ProductID == pro.Id)
                    {
                        pw.ProductID = cp.ProductID;
                        pw.CustomerID = cp.CustomerID;
                        pw.ProductByName = cp.ProductByName;
                        pw.PriceCustomer = cp.Price;
                    }
                }

                pViewList.Add(pw);
            }

            return pViewList;
        }

        /// <summary>
        /// 查询客户已关联产品
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public List<ProductView> GetCustomerProList(Guid cusID)
        {
            return _customerProductRepository.GetCustomerProList(cusID).ToList();
        }

        /// <summary>
        /// 查询客户购买过的所有产品[去重取最新定价]
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public List<ProductView> GetCustomerOrderProductList(Guid cusID)
        {
            List<ProductView> ls = new List<ProductView>();

            List<ProductView> lsAllPro = (_customerProductRepository.GetCustomerOrderProductList(cusID)).ToList();

            //获取所有的productID
            List<Guid> lsID = new List<Guid>();
            for (int i = 0; i < lsAllPro.Count; i++)
            {
                lsID.Add(lsAllPro[i].ProductID);
            }

            //productID 去重
            HashSet<Guid> lsNewID = new HashSet<Guid>(lsID);

            //循环获取最新审核的ProductView列表
            foreach (var obj in lsNewID)
            {
                ProductView tmp = new ProductView();
                tmp.PriceTime = Convert.ToDateTime("2017-1-1");
                for (int j = 0; j < lsAllPro.Count; j++)
                {
                    if (obj == lsAllPro[j].ProductID)
                    {
                        if (lsAllPro[j].PriceTime > tmp.PriceTime)
                        {
                            tmp.Amount = lsAllPro[j].Amount;
                            tmp.SendAmount= lsAllPro[j].SendAmount;
                            tmp.CustomerID = lsAllPro[j].CustomerID;
                            tmp.FinalPrice = lsAllPro[j].FinalPrice;
                            tmp.Id = lsAllPro[j].Id;
                            tmp.Name = lsAllPro[j].Name;
                            tmp.Number = lsAllPro[j].Number;
                            tmp.OrderProductID = lsAllPro[j].OrderProductID;
                            tmp.Price = lsAllPro[j].Price;
                            tmp.PriceCustomer = lsAllPro[j].PriceCustomer;
                            tmp.PriceTime = lsAllPro[j].PriceTime;
                            tmp.ProductByName = lsAllPro[j].ProductByName;
                            tmp.ProductID = lsAllPro[j].ProductID;
                            //tmp.Type = lsAllPro[j].Type;
                        }
                    }
                }
                ls.Add(tmp);
            }

            return ls;
        }


        /// <summary>
        /// 添加客户产品关联
        /// </summary>
        /// <param name="ls"></param>
        /// <returns></returns>
        public int AddList(List<CustomerProduct> ls)
        {
            return _customerProductRepository.Insert(ls);
        }

        /// <summary>
        /// 删除客户与产品的关联
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public int DeleteRelated(Guid cusID)
        {
            return _customerProductRepository.Delete(a => a.CustomerID == cusID);
        }
    }
}
